import 'dart:io';


import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ahj_app/base/basewidget/viewmodel/ViewModelProvider.dart';
import 'package:flutter_ahj_app/base/cunchu/Constant.dart';
import 'package:flutter_ahj_app/base/cunchu/UserInfoUtils.dart';
import 'package:flutter_ahj_app/base/utils/downloadfile/sp_util.dart';
import 'package:flutter_ahj_app/base/utils/screen_util.dart';
import 'package:flutter_ahj_app/root_scene.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'base/values/SQColor.dart';
import 'base/utils/mineWebView.dart';
import 'package:flutter_ahj_app/base/finClip/FcSdk.dart';
class AppScene1 extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => AppSceneState();
}

class AppSceneState extends State<AppScene1> {
  bool isInitModule = false;
  BuildContext mContext;

  @override
  initState() {
    super.initState();
    _init();
  }

  getDeviceMessage() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    await FcSdk.init();
    UserInfoUtils.setPackageVeName(packageInfo.version);
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    if (Platform.isAndroid) {
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;

      UserInfoUtils.setDeVeName(
          androidInfo.brand + androidInfo.version.release);
    } else {
      IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
      UserInfoUtils.setDeVeName(iosInfo.name + iosInfo.systemVersion);
    }
  }

  _init() async {
    await SpUtil.getInstance();
    await UserInfoUtils.getInstance();

    if (isEmpty(UserInfoUtils.getUserYinSi())) {
      // 提示框
      Future.delayed(Duration.zero, () {
        _askedYinsi(context);
      });
    } else {
      if (Constant.getFirstOpen()) {
        //首次登录
        

      } else {
        getDeviceMessage();
        //非首次登录
        setState(() {
          body = RootScene();
        });
      }
    }
  }

  Widget body;


  /*
  * 判断字段是不是null
  * */
  bool isEmpty(Object value) {
    if (value == null || value == "") return true;
    if (value is String && value.isEmpty) {
      return true;
    }
    return false;
  }

  _askedYinsi(BuildContext context) async {
    StatefulBuilder sb = StatefulBuilder(
      builder: (context, mSetState) {
        return WillPopScope(
          child: new AlertDialog(
            title: new Text("用户协议与隐私政策"),
            content: Container(
              child: Text.rich(
                TextSpan(
                  children: [
                    TextSpan(
                      text: " 感谢您对本公司的支持!本公司非常重视您的个人信息和隐私保护。" +
                          "为了更好地保障您的个人权益，在您使用我们的产品前，" +
                          "请务必审慎阅读",
                      style:
                          TextStyle(fontSize: 14, color: SQColor.black_333333),
                    ),
                    TextSpan(
                      text: "《用户隐私政策》",
                      style: TextStyle(fontSize: 14, color: SQColor.red),
                      // 设置点击事件
                      recognizer: TapGestureRecognizer()
                        ..onTap = () {
                          Navigator.push(
                              context,
                              new MaterialPageRoute(
                                  builder: (context) => MineWebView("用户隐私协议",
                                      "https://ahj-static.oss-cn-beijing.aliyuncs.com/html/index.html")));
//                          WebViewUntil
//                          Navigator.pushNamed(context, "/WebViewUntil",
//                              arguments: {"title":  "用户隐私协议", "url": "https://ahj-static.oss-cn-beijing.aliyuncs.com/html/index.html"});

//                          Navigator.push(
//                              context,
//                              new MaterialPageRoute(
//                                  builder: (context) => MineWebView(
//                                      "用户隐私协议",
//                                      "https://ahj-static.oss-cn-beijing.aliyuncs.com/html/xinanyun-agreement.html")));
                        },
                    ),
                  ],
                ),
              ),
            ),
            actions: <Widget>[
              new FlatButton(
                  child: new Text("不同意",
                      style: new TextStyle(
                          fontSize: 14, color: SQColor.black_333333)),
                  onPressed: () {
                    Navigator.of(context).pop();
                    exit(0);
                  }),
              new FlatButton(
                  child: Text("同意并继续",
                      style: new TextStyle(fontSize: 14, color: SQColor.blue)),
                  onPressed: () {
                    UserInfoUtils.setUserYinSi("112313");

                    if (Constant.getFirstOpen()) {
                      setState(() {
                        body = RootScene();
                      });
                      Constant.setFirstOpen(false);
                    } else {
                      //非首次登录
                      setState(() {
                        body = RootScene();
                      });
                    }
                    Navigator.of(context).pop();
                  })
            ],
          ),
          onWillPop: () async {
            return Future.value(false);
          },
        );
      },
    );

    showDialog(
        context: context, barrierDismissible: false, builder: (context) => sb);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: body == null
          ? new Container(
              child: new Container(
                // padding: EdgeInsets.fromLTRB(0, 15, 15, 0),
                // color: SQColor.white,
                child: Stack(
                    // crossAxisAlignment:CrossAxisAlignment.center,
                  children: [

                    Container(
                      alignment: Alignment.bottomCenter,
                      child: new Image.asset('img/ic_qn_launch.png',
                        width: ScreenUtil.getScreenW(context),
                        height: ScreenUtil.getScreenH(context),
                      ),
                    ),

                  ],
                )
              ),
            )
          : body,
    );
  }
}
